package com.bluemartini.jmx.mbean;

import com.bluemartini.dna.DNAList;

/**
 * Expose JCS cache statistics as MBean for management
 * 
 * @author Yannick Robin
 *
 */
public class JCSCache implements JCSCacheMBean {
	
	private String name;
	public JCSCache(String name)
	{
		this.name = name;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public int getHitCountAux() {
		DNAList dnaCache = Cache.getCacheStats(name);
		if (dnaCache == null)
			return 0;
		String sValue = dnaCache.getString("HitCountAux");
		if (sValue == null || sValue.equals(""))
			return 0;
		else
			return Integer.parseInt(sValue);
	}

	/**
	 * {@inheritDoc}
	 */
	public int getHitCountRam() {
		DNAList dnaCache = Cache.getCacheStats(name);
		if (dnaCache == null)
			return 0;
		String sValue = dnaCache.getString("HitCountRam");
		if (sValue == null || sValue.equals(""))
			return 0;
		else
			return Integer.parseInt(sValue);
		
	}

	/**
	 * {@inheritDoc}
	 */
	public double getHitPercent() {
		DNAList dnaCache = Cache.getCacheStats(name);
		if (dnaCache == null)
			return 0;
		return dnaCache.getDouble("hit_percent", 0);
	}

	/**
	 * {@inheritDoc}
	 */
	public int getListSize() {
		DNAList dnaCache = Cache.getCacheStats(name);
		if (dnaCache == null)
			return 0;
		String sValue = dnaCache.getString("List Size");
		if (sValue == null || sValue.equals(""))
			return 0;
		else
			return Integer.parseInt(sValue);
	}

	/**
	 * {@inheritDoc}
	 */
	public int getMapSize() {
		DNAList dnaCache = Cache.getCacheStats(name);
		if (dnaCache == null)
			return 0;
		String sValue = dnaCache.getString("Map Size");
		if (sValue == null || sValue.equals(""))
			return 0;
		else
			return Integer.parseInt(sValue);
	}

	/**
	 * {@inheritDoc}
	 */
	public int getMaxSize() {
		DNAList dnaCache = Cache.getCacheStats(name);
		if (dnaCache == null)
			return 0;
		return dnaCache.getInteger("maxSize", 0);
	}

	/**
	 * {@inheritDoc}
	 */
	public int getMissCount() {
		DNAList dnaCache = Cache.getCacheStats(name);
		if (dnaCache == null)
			return 0;
		String sValue = dnaCache.getString("Miss Count");
		if (sValue == null || sValue.equals(""))
			return 0;
		else
			return Integer.parseInt(sValue);
	}

	/**
	 * {@inheritDoc}
	 */
	public int getNumberOfNoWaits() {
		DNAList dnaCache = Cache.getCacheStats(name);
		if (dnaCache == null)
			return 0;
		String sValue = dnaCache.getString("Number of No Waits");
		if (sValue == null || sValue.equals(""))
			return 0;
		else
			return Integer.parseInt(sValue);
	}

	/**
	 * {@inheritDoc}
	 */
	public int getPutCount() {
		DNAList dnaCache = Cache.getCacheStats(name);
		if (dnaCache == null)
			return 0;
		String sValue = dnaCache.getString("Put Count");
		if (sValue == null || sValue.equals(""))
			return 0;
		else
			return Integer.parseInt(sValue);
	}

	/**
	 * {@inheritDoc}
	 */
	public int getSize() {
		DNAList dnaCache = Cache.getCacheStats(name);
		if (dnaCache == null)
			return 0;
		return dnaCache.getInteger("size", 0);
	}

	/**
	 * {@inheritDoc}
	 */
	public int getHitCount() {
		DNAList dnaCache = Cache.getCacheStats(name);
		if (dnaCache == null)
			return 0;
		String sValue = dnaCache.getString("Hit Count");
		if (sValue == null || sValue.equals(""))
			return 0;
		else
			return Integer.parseInt(sValue);
	}

}
